#include "videostream.h"
#include <unistd.h>
#include <sys/time.h>
#include <thread>

void threadCameraPlay()
{
	VStream vstreamClient;
	while (1)
	{
		int ret;
		if ((ret = vstreamClient.OpenCamera("rtsp://10.0.97.103:554", "admin", "wavecamera1")) == VS_OK) {
			printf("10.0.97.102 :---> OpenCamera Success.\n");
			break;
		}
		else {
			vstreamClient.Close();
			if (ret == VS_NO_H264)
			{
				printf("No H264.\n");
				return;
			}
			continue;
		}
	}
	cv::Mat frameMat;
	while (1) {
		vstreamClient.GetFrameData(&frameMat);
		if (!frameMat.empty()) {
			//cv::resize(frameMat, showMat, cv::Size(1280, 720), 0, 0);
			//cv::imshow("gdb", showMat);
			printf("10.0.97.102 :---> w: %5d, h: %5d\n", frameMat.cols, frameMat.rows);
		}
		cv::waitKey(30);
	}
	vstreamClient.Close();
}

void threadCameraPlay1()
{
	VStream vstreamClient;
	while (1)
	{
		int ret;
		if ((ret = vstreamClient.OpenCamera("rtsp://10.0.66.203:554", "admin", "wavecamera1")) == VS_OK) {
			printf("10.0.66.203 :---> OpenCamera Success.\n");
			break;
		}
		else {
			vstreamClient.Close();
			if (ret == VS_NO_H264)
			{
				printf("No H264.\n");
				return;
			}
			continue;
		}
	}
	cv::Mat frameMat;
	while (1) {
		vstreamClient.GetFrameData(&frameMat);
		if (!frameMat.empty()) {
			//cv::resize(frameMat, showMat, cv::Size(1280, 720), 0, 0);
			//cv::imshow("gdb", showMat);
			printf("10.0.66.203 :---> w: %5d, h: %5d\n", frameMat.cols, frameMat.rows);
		}
		cv::waitKey(30);
	}
	vstreamClient.Close();
}

void threadCameraPlay2()
{
	VStream vstreamClient;
	while (1)
	{
		int ret;
		if ((ret = vstreamClient.OpenCamera("rtsp://10.0.63.201:554", "admin", "wavecamera1")) == VS_OK) {
			printf("10.0.63.200 :---> OpenCamera Success.\n");
			break;
		}
		else {
			vstreamClient.Close();
			if (ret == VS_NO_H264)
			{
				printf("No H264.\n");
				return;
			}
			continue;
		}
	}
	cv::Mat frameMat;
	while (1) {
		vstreamClient.GetFrameData(&frameMat);
		if (!frameMat.empty()) {
			//cv::resize(frameMat, showMat, cv::Size(1280, 720), 0, 0);
			//cv::imshow("gdb", showMat);
			printf("10.0.63.200 :---> w: %5d, h: %5d\n", frameMat.cols, frameMat.rows);
		}
		cv::waitKey(30);
	}
	vstreamClient.Close();
}

int main()
{

	std::thread threadCmaera1(threadCameraPlay);
	threadCmaera1.detach();
	std::thread threadCmaera2(threadCameraPlay1);
	threadCmaera2.detach();
	std::thread threadCmaera3(threadCameraPlay2);
	threadCmaera3.detach();
	for (;;);
}
